package com.cgtong.android.util;

import android.net.Uri;

import com.cgtong.android.http.ConstantManager;
import com.jph.takephoto.app.TakePhoto;
import com.jph.takephoto.compress.CompressConfig;
import com.jph.takephoto.model.CropOptions;
import com.jph.takephoto.model.LubanOptions;
import com.jph.takephoto.model.TakePhotoOptions;

import java.io.File;

public class TakePhotoHelper {

    public void onClick(TakePhoto takePhoto, boolean takeFromLocal, boolean isCompress, boolean isCrop) {
        File imgDir = new File(ConstantManager.PHOTO_SAVE_DIR);
        if (!imgDir.exists()) {
            imgDir.mkdirs();
        }
        File file = new File(imgDir, System.currentTimeMillis() + ".png");
        if (!file.getParentFile().exists()) file.getParentFile().mkdirs();
        Uri imageUri = Uri.fromFile(file);

        configCompress(takePhoto, isCompress);
        configTakePhotoOption(takePhoto);
        if (takeFromLocal) {
            //-----------本地获取
            int limit = 1;
//            if (isCrop) {
//                takePhoto.onPickMultipleWithCrop(limit, getCropOptions(isCrop));
//            } else {
//                takePhoto.onPickMultiple(limit);
//            }
            //从文件管理
//                    takePhoto.onPickFromDocumentsWithCrop(imageUri, getCropOptions());
//                    takePhoto.onPickFromDocuments();
            //从相册
            if (isCrop) {
                takePhoto.onPickFromGalleryWithCrop(imageUri, getCropOptions(isCrop));
            } else {
                takePhoto.onPickFromGallery();
            }
        } else {
            //-----------拍照获取
            if (isCrop) {
                takePhoto.onPickFromCaptureWithCrop(imageUri, getCropOptions(isCrop));
            } else {
                takePhoto.onPickFromCapture(imageUri);
            }
        }
    }

    private void configTakePhotoOption(TakePhoto takePhoto) {
        TakePhotoOptions.Builder builder = new TakePhotoOptions.Builder();
//        builder.setWithOwnGallery(false);
        builder.setCorrectImage(true);
        takePhoto.setTakePhotoOptions(builder.create());

    }

    private static void configCompress(TakePhoto takePhoto, boolean isCompress) {
        if (!isCompress) {
            takePhoto.onEnableCompress(null, false);
            return;
        }
        int maxSize = 102400;
        int width = 0;
        int height = 0;
        CompressConfig config;
        //使用自带压缩工具
//        config = new CompressConfig.Builder()
//                .setMaxSize(maxSize)
////                .setMaxPixel(width >= height ? width : height)
//                .enableReserveRaw(false)//拍照压缩后是否保存原图
//                .create();
        //使用鲁班压缩工具
        LubanOptions option = new LubanOptions.Builder()
                .setMaxHeight(height)
                .setMaxWidth(width)
                .setMaxSize(maxSize)
                .create();
        config = CompressConfig.ofLuban(option);
        config.enableReserveRaw(true);
        takePhoto.onEnableCompress(config, false);


    }

    private static CropOptions getCropOptions(boolean isCrop) {
        if (!isCrop) return null;
        boolean withWonCrop = true;
        CropOptions.Builder builder = new CropOptions.Builder();
        builder.setAspectX(800).setAspectY(800);
        builder.setWithOwnCrop(withWonCrop);
        return builder.create();
    }
}
